package com.shop.loan.util;

import cn.hutool.core.codec.Base64;

import java.io.*;

public class FileUtil {

//    private static final String pdfPath = "/data/file/pdf";
//    private static final String imagePath = "/data/file/image";

//    private static final String pdfPath = "/Users/ddc/Downloads";
//    private static final String imagePath = "/Users/ddc/Downloads";


//    public static String pdf2Image(String pdfFile) {
//        String saveFilePath = pdfPath + "/" + UUID.randomUUID() + ".jpg";
//        try (PDDocument pdf = PDDocument.load(new FileInputStream(pdfFile))) {
//            int actSize = pdf.getNumberOfPages();
//            List<BufferedImage> picList = Lists.newArrayList();
//            for (int i = 0; i < actSize; i++) {
//                BufferedImage image = new PDFRenderer(pdf).renderImageWithDPI(i, 150, ImageType.RGB);
//                picList.add(image);
//            }
//            // 组合图片
//            ImageUtil.yPic(picList, saveFilePath);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return saveFilePath;
//    }

    /**
     * pdf文件转图片
     *
     * @param filePath
     */
//    public static String pdf2Image(String filePath) {
//        String fileType = "jpg";
//        String saveFilePath = pdfPath + "/" + UUID.randomUUID() + "." + fileType;
//        try {
//            // 将pdf装图片 并且自定义图片得格式大小
//            File file = new File(filePath);
//            PDDocument doc = PDDocument.load(file);
//            PDFRenderer renderer = new PDFRenderer(doc);
//            int pageCount = doc.getNumberOfPages();
//            for (int i = 0; i < pageCount; i++) {
//                BufferedImage image = renderer.renderImageWithDPI(i, 144); // Windows native DPI
//                // BufferedImage srcImage = resize(image, 240, 240);//产生缩略图
//                ImageIO.write(image, fileType, new File(saveFilePath));
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return saveFilePath;
//    }


//    /**
//     * 文件下载到本地
//     */
//    public static String download(String fileUrl, String fileType) {
//        String saveFilePath = imagePath + "/" + UUID.randomUUID() + "." + fileType;
//        //将文件下载后保存在E盘，返回结果为下载文件大小
//        long size = HttpUtil.downloadFile(fileUrl, cn.hutool.core.io.FileUtil.file(saveFilePath));
//        return saveFilePath;
//    }


    /**
     * 图片转化成base64字符串
     *
     * @param imgPath
     * @return
     */
    public static String getImageStr(String imgPath) {
        // 待处理的图片
        InputStream in = null;
        byte[] data = null;
        // 返回Base64编码过的字节数组字符串
        String encode = null;
        // 对字节数组Base64编码
        Base64 encoder = new Base64();
        try {
            // 读取图片字节数组
            in = new FileInputStream(imgPath);
            data = new byte[in.available()];
            in.read(data);
            encode = encoder.encode(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        encode = encode.replaceAll("\n", "");
        return encode;
    }


    /**
     * base64转为图片
     *
     * @param path   文件路径：到文件夹即可，代码里会在文件夹里生成对应的jpg文件
     * @param base64
     * @return
     */
    public static String generateImage(String base64, String path,String fileName) {
        // 判断文件路径是否存在
        File filePath = new File(path);
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        // 创建文件
        String jpgFile = path + "/" + fileName;
        File file = new File(jpgFile);
        boolean jpgFileExist = false;
        try {
            jpgFileExist = file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (jpgFileExist) {
            // 解密
            Base64 decoder = new Base64();
            // 去掉base64前缀 data:image/jpeg;base64,
            base64 = base64.substring(base64.indexOf(",", 1) + 1, base64.length());
            try {
                byte[] b = decoder.decode(base64);
                // 处理数据
                for (int i = 0; i < b.length; ++i) {
                    if (b[i] < 0) {
                        b[i] += 256;
                    }
                }
                // 保存图片
                FileOutputStream out = new FileOutputStream(jpgFile);
                out.write(b);
                out.flush();
                out.close();
                // 写入成功返回文件路径
                return jpgFile;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}
